<?php

declare(strict_types=1);

namespace app\validate\verify;

use think\Validate;

class FutureSellerReplyValidate extends Validate
{
    protected $rule = [
        'status' => 'in:1,2',
        'seller_price' => 'float|gt:0',
        'seller_delivery_date' => 'date',
        'seller_remark' => 'max:255',
        'seller_media' => 'checkMedia',
    ];

    protected $message = [
        'status.in' => '状态不合法',
        'seller_price.float' => '报价格式不正确',
        'seller_price.gt' => '报价必须大于0',
        'seller_delivery_date.date' => '交期格式不正确',
        'seller_remark.max' => '卖家备注不能超过255个字符',
    ];

    protected $scene = [
        'reply' => ['status', 'seller_price', 'seller_delivery_date', 'seller_remark', 'seller_media'],
    ];

    protected function sceneReply(): self
    {
        return $this->only($this->scene['reply'])
            ->append('seller_price', 'requireIf:status,1')
            ->append('seller_delivery_date', 'requireIf:status,1');
    }

    protected function checkMedia($value)
    {
        if ($value === null || $value === '') {
            return true;
        }

        if (!is_array($value) && !is_string($value)) {
            return '附件格式不正确';
        }

        return true;
    }
}
